<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kategori extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Kategori_model');
			}

	function index(){
			$offset = $this->uri->segment(3);
			$config['base_url'] = $this->config->site_url().'/kategori/index';
			$config['total_rows'] = $this->db->count_all('tb_kategori');
			$config['per_page'] = 10;
			$config['uri_segment'] = '3';		
				
		$data['urutan'] = $this->uri->segment(3);	
		if(($this->session->userdata('username') == TRUE) && ($this->session->userdata('rule') == '1' )):
			$data['title'] = 'Data Kategori';
			$data['status'] = 'Data Kategori';
			$data['query'] = $this->Kategori_model->getKategori('list',FALSE,$config['per_page'],$offset);
			$data['main_view'] = 'kategori/kategori';
			$this->pagination->initialize($config);
			$this->load->view('index',$data);
		else:
			$data['title'] = 'Login';
			$this->load->view('login',$data);
		endif;

	}


	function login_process()
	{
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');

		if ($this->form_validation->run() == FALSE):
			$data['title'] = 'Login';
			$this->load->view('login',$data);
		else:
			$username = $this->input->post('username');
			$password = md5($this->input->post('password'));
			$this->db->where('username',$username);
			$this->db->where('password',$password);
			$res = $this->db->get('tb_admin')->result();

			if (count($res) > 0):
				$this->session->set_userdata('rule',$res[0]->rule );
				$this->session->set_userdata('username',$res[0]->username );
				redirect('kategori','refresh');
			else:
				$thx='<div id="pesan">Maaf Username atau password anda salah</div>';
				$this->session->set_flashdata('message_type',$thx);
				redirect('kategori','refresh');
			endif;
		endif;
	}

	function logout(){
		$this->session->unset_userdata('rule');
		$this->session->unset_userdata('username');
		//$this->session->sess_destroy();
		redirect('kategori','refresh');
	}


	function add(){
		$this->form_validation->set_rules('kategori','nama matkul','required');
			if ($this->form_validation->run() == FALSE){
		$data['title'] = 'Tambah Kategori';
		$data['status'] = 'Add Data Kategori';
		$data['main_view'] = 'kategori/add';
		$this->load->view('index',$data);
			}
			else {
				$data = array('nama_kategori'=> $this->input->post('kategori'));
				
				$this->Kategori_model->addKategori($data);
				redirect('kategori');
			}
	}
	
	function edit(){
		$this->form_validation->set_rules('kategori','kategori','required');
				if ($this->form_validation->run() == FALSE) {
					$id_kategori = $this->uri->segment(3);
				$data['title'] = 'Edit Kategori';
				$data['status'] = 'Edit Data Kategori';
				$data['row'] = $this->Kategori_model->getKategori('by_id',$id_kategori,FALSE,FALSE,FALSE);
				$data['main_view'] = 'kategori/edit';
				$this->load->view('index',$data);
			}
			else {
			$id = $this->uri->segment(3);
				$data = array('nama_kategori'=> $this->input->post('kategori'));
				$this->Kategori_model->editKategori($id,$data);
				redirect('kategori');
			}
	}
	function delete(){
		$id = $this->uri->segment(3);
		$this->Kategori_model->deleteKategori($id);
		redirect('kategori');
	
	}
	
}

?>